<?php
/**
 * 検索APIコントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Api_Search extends Controller_Apibase {
	/*
	 * 件数取得
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_facet() {
		$post = Input::post();
		$json = array();

		$kind = $post['kind'];
		$word = $post['word'];

		// キャッシュ有無
		try {
			$ckey = $this -> ckey('search', 'facet', $kind, $word);
			$json = Cache::get($ckey);
			Log::debug('Cache Hit');

		} catch (\CacheNotFoundException $e) {
			if ($kind != 2) {
				// 楽曲検索
				// 曲DBが少ない場合に備え、iTunesのデータを加える
				$this->get_itunesapi($word);
					
				$match = array('artist', 'song_title');
				$query = DB::select(DB::expr('COUNT(*) AS count'));
				$query -> from('song');
				$query -> where(DB::expr('MATCH(' . $match[$kind] . ')'), '', DB::expr("AGAINST( " . DB::escape($word) . " IN BOOLEAN MODE)"));
				$result = $query -> execute() -> as_array();
			} else {
				// コメント検索
				$query = DB::select(DB::expr('COUNT(*) AS count'));
				$query -> from('comment');
				$query -> where('del_flg', 0);
				$query -> where(DB::expr('MATCH(word)'), '', DB::expr("AGAINST( " . DB::escape($word) . " IN BOOLEAN MODE)"));
				$query -> group_by('song_id');
				$result = $query -> execute() -> as_array();
			}

			// レスポンス
			$count = isset($result[0]) ? $result[0]['count'] : 0;
			$json = array("comment_count" => $count, 'kind' => $kind, 'word' => $word);

			// キャッシュ保存
			$json = json_encode($json);
			Cache::set($ckey, $json, Config::get('cache_search_expiration'));
		}

		return Response::forge($json, D_HTTP_CACHE);
	}

	/**
	 * 楽曲検索
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_song() {
		$post = Input::post();
		$result = array();

		$kind = $post['kind'];
		$word = $post['word'];

		// キャッシュ有無
		try {
			$ckey = $this -> ckey('search', 'song', $kind, $word);
			$json = Cache::get($ckey);
			Log::debug('Cache Hit');

		} catch (\CacheNotFoundException $e) {
			if ($kind != 2) {
				// 楽曲検索
				// 曲DBが少ない場合に備え、iTunesのデータを加える
				$this->get_itunesapi($word);
					
				$match = array('song.artist', 'song.song_title');
				$query = DB::select('song.song_title', 'song.artist', DB::expr('count_comment AS score'), 'artwork', 'preview');
				$query -> from('song');
				$query -> where(DB::expr('MATCH(' . $match[$kind] . ')'), '', DB::expr("AGAINST( " . DB::escape($word) . " IN BOOLEAN MODE)"));
				$query -> join('itunes_song', 'LEFT') -> on('song.itunes_track_id', '=', 'itunes_song.itunes_track_id');
				$query -> order_by("count_comment", "desc");
				$query -> limit(Config::get('maxrows_searchlist'));
				$json = $query -> execute() -> as_array();
			} else {
				// コメント検索
				$query = DB::select('song.song_title', 'song.artist', DB::expr('count_comment AS score'), 'artwork', 'preview');
				$query -> from('comment');
				$query -> where('del_flg', 0);
				$query -> where(DB::expr('MATCH(word)'), '', DB::expr("AGAINST( " . DB::escape($word) . " IN BOOLEAN MODE)"));
				$query -> join('song') -> on('comment.song_id', '=', 'song.song_id');
				$query -> join('itunes_song', 'LEFT') -> on('song.itunes_track_id', '=', 'itunes_song.itunes_track_id');
				$query -> group_by('comment.song_id');
				$query -> order_by("song.count_comment", "desc");
				$query -> limit(Config::get('maxrows_searchlist'));
				$json = $query -> execute() -> as_array();
			}

			// キャッシュ保存
			$json = json_encode($json);
			Cache::set($ckey, $json, Config::get('cache_search_expiration'));
		}

		return Response::forge($json, D_HTTP_CACHE);
	}

	/*
	 * アーティスト検索
	 *
	 */
	public function action_artist() {
		$post = Input::post();
		$result = array();

		$kind = $post['kind'];
		$word = $post['word'];

		// キャッシュ有無
		try {
			$ckey = $this -> ckey('search', 'artist', $kind, $word);
			$json = Cache::get($ckey);
			Log::debug('Cache Hit');

		} catch (\CacheNotFoundException $e) {
			if ($kind != 2) {
				// 楽曲検索
				// 曲DBが少ない場合に備え、iTunesのデータを加える
				$this->get_itunesapi($word);

				$match = array('artist', 'song_title');
				$query = DB::select('artist', DB::expr('COUNT(*) AS count_song'), DB::expr('SUM(count_comment) AS count_comment'));
				$query -> from('song');
				$query -> where(DB::expr('MATCH(' . $match[$kind] . ')'), '', DB::expr("AGAINST( " . DB::escape($word) . " IN BOOLEAN MODE)"));
				$query -> group_by('artist');
				$query -> order_by("count_comment", "desc");
				$json = $query -> execute() -> as_array();
			} else {
				// コメント検索
				$query = DB::select('artist', DB::expr('COUNT(*) AS count_song'), DB::expr('SUM(count_comment) AS count_comment'));
				$query -> from('comment');
				$query -> where('del_flg', 0);
				$query -> where(DB::expr('MATCH(word)'), '', DB::expr("AGAINST( " . DB::escape($word) . " IN BOOLEAN MODE)"));
				$query -> join('song') -> on('comment.song_id', '=', 'song.song_id');
				$query -> group_by('song.artist');
				$query -> order_by("song.count_comment", "desc");
				$query -> limit(Config::get('maxrows_searchlist'));
				$json = $query -> execute() -> as_array();
			}

			// キャッシュ保存
			$json = json_encode($json);
			Cache::set($ckey, $json, Config::get('cache_search_expiration'));
		}

		return Response::forge($json, D_HTTP_CACHE);
	}

	/**
	 * iTunes検索
	 *
	 * @access  public
	 * @return  Response
	 */
	public function action_itunes() {
		$post = Input::post();
		$json = array();

		$word = $post['word'];

		// iTunes検索
		$json = $this -> get_itunesapi($word);

		return Response::forge(json_encode($json), D_HTTP_CACHE);
	}

}
